//
//  PartyerShowController.m
//  UESTCPioneer
//
//  Created by Apple on 14-9-25.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "PartyerShowController.h"
#import "PartyerShowCell.h"
#import "PartyerShowDetailController.h"
#import "PartyerShowEntity.h"

@interface PartyerShowController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray * _heightArray;
    NSInteger  _idMin;
}
@end

@implementation PartyerShowController

- (id)initWithRequestData:(NSDictionary *)RequestData entityName:(NSString *)EntityName Mapping:(NSDictionary *)Mapping{
    if (self) {
        self.title = @"党员风采";
        requestData = RequestData;
        entityName = EntityName;
        entityMapping = Mapping;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
//    tableView.dataSource = self;
//    tableView.delegate = self;
//    [self.view addSubview:tableView];
    
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorInset = UIEdgeInsetsZero;
    [self.view addSubview:self.tableView];
    self.title = @"党员风采";
    _heightArray = [[NSMutableArray alloc] init];
    _idMin = 10000;
//    [self getServerData];
}

-(void)getServerData{

    NSDictionary * dict = @{@"tyep":@"excellentMember",@"page":@"1",@"idMin":@"1"};
    [NetworkCenter RKRequestWithData:dict EntityName:@"PartyerShowEntity" Mapping:[Mapping PartyerShowEntityMapping] SuccessBlock:^(NSArray *resultArray) {
        NSMutableArray * dic = [[NSMutableArray alloc] init];
        [dic addObjectsFromArray:resultArray];
        tableViewEntitiesArray = [[NSArray alloc] initWithArray:dic];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"加载失败");
        [Alert showAlert:@"加载失败!"];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [tableViewEntitiesArray count];
//    return 10;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * CellIdentifier = @"Cell";
    PartyerShowCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[PartyerShowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    PartyerShowEntity * entity = tableViewEntitiesArray[indexPath.row];
    if ([entity.theId integerValue]<_idMin) {
        _idMin = [entity.theId integerValue];
    }
    
    cell.imageName = [NSString stringWithFormat:@"%@%@",baseUrl,entity.picUrl];
    
    CGFloat x = CGRectGetMaxX(cell.headImageView.frame) + 10;
    CGFloat w = cell.frame.size.width - 50 - cell.headImageView.frame.size.width;
    CGFloat h = [_heightArray[indexPath.row] floatValue] - 20;
    
    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10, w, h)];
    [cell setContentLabel:contentLabel];
    cell.contentLabel.text = entity.brief;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    [_heightArray addObject:[NSString stringWithFormat:@"%f",90.0f]];
    return 90.0f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PartyerShowDetailController * controller = [[PartyerShowDetailController alloc] initWithEntity:tableViewEntitiesArray[indexPath.row]];
    [self.navigationController pushViewController:controller animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)pullUpRefresh:(MJRefreshBaseView *)refreshView{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:requestData];
    [dic setObject:[NSString stringWithFormat:@"%d",PullUpRefreshTimes+2] forKey:@"page"];
    [dic setObject:[NSString stringWithFormat:@"%d",_idMin] forKey:@"idMin"];
    NSLog(@"dic-= %@",dic);
    [NetworkCenter RKRequestWithData:dic EntityName:entityName Mapping:entityMapping SuccessBlock:^(NSArray *resultArray) {
        
        if ([resultArray count]==0){
            [refreshView endRefreshing];
            return ;
        }
        
        NSMutableArray *newArray = [[NSMutableArray alloc]init];
        [newArray addObjectsFromArray:tableViewEntitiesArray];
        [newArray addObjectsFromArray:resultArray];
        NSArray *result = [[NSArray alloc]initWithArray:newArray];

        tableViewEntitiesArray=result;
        
        [self.tableView reloadData];
        
        //清除core data 因为现在还没有适合下拉刷新和适合本地缓存的api
        
        [refreshView endRefreshing];
        PullUpRefreshTimes++;
        
    } failure:^(NSError *error) {
        [refreshView endRefreshing];
        [Alert showAlert:@"上拉刷新失败!"];
    }];
}


@end
